package miharu;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;

/**
 * CSVデータを扱うユーティリティクラス。
 *
 * @author yasuhiko.sakaida@gmail.com
 *
 */
public class CSVUtil {

    /**
     * 引数で与えらえrたCSVファイルから情報を読み取ってDenryokuBeanオブジェクトのList形式に変換して返します。
     * @return List形式になったDenryokuBean
     * @throws IOException
     */
	public static List<DenryokuBean> getDenryokuList(FileReader csvFile) throws IOException {
        ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
        strat.setType(DenryokuBean.class);
        // CSVの順番で、どのフィールドにマッピングすればいいかを指定する。
        String[] columns = new String[] {"date", "time", "today" , "yesterday"};
        strat.setColumnMapping(columns);

        CsvToBean csv = new CsvToBean();


        return csv.parse(strat, csvFile);
	}




	/**
	 * 引数で与えらえられた配列から情報を読み取ってDenryokuBeanオブジェクトのList形式に変換して返します。
	 *
	 * @param String[] csvDatas
	 * 引数で与えるcsvDatasは以下のような例の形式である必要がある。
	 * 		String[] csvDatas =
		{
		"2011/3/26,0:00,2889,2990",
		"2011/3/26,1:00,2758,2848",
		"2011/3/26,2:00,2674,2767",
		"2011/3/26,3:00,2603,2707",
		"2011/3/26,4:00,2572,2696",
		"2011/3/26,5:00,2610,2828",
		"2011/3/26,6:00,2715,3095",
		"2011/3/26,7:00,2883,3351",
		"2011/3/26,8:00,3031,3500",
		"2011/3/26,9:00,3119,3575",
		"2011/3/26,10:00,3112,3491",
		"2011/3/26,11:00,3088,3435",
		"2011/3/26,12:00,2982,3223",
		"2011/3/26,13:00,3015,3329",
		"2011/3/26,14:00,2976,3318",
		"2011/3/26,15:00,2960,3303",
		"2011/3/26,16:00,3011,3388",
		"2011/3/26,17:00,3140,3451",
		"2011/3/26,18:00,3433,3480",
		"2011/3/26,19:00,3462,3283",
		"2011/3/26,20:00,0,3344",
		"2011/3/26,21:00,0,3261",
		"2011/3/26,22:00,0,3241",
		"2011/3/26,23:00,0,3080"
		};
	 * @return List形式になったDenryokuBean
	 */
	public static List<DenryokuBean> getDenryokuList(String[] csvDatas) {
		List<DenryokuBean> returnBean = new ArrayList<DenryokuBean>();
		for(String csvData : csvDatas){
			String[] tmp = csvData.split(",");

			DenryokuBean tmpBean = new DenryokuBean();

			tmpBean.setDate(tmp[0]);
			tmpBean.setTime(tmp[1]);
			tmpBean.setToday(tmp[2]);
			tmpBean.setYesterday(tmp[3]);

			returnBean.add(tmpBean);

		}

		return returnBean;
	}


	public static void main(String args[]){

		String[] csvDatas =
		{
		"2011/3/26,0:00,2889,2990",
		"2011/3/26,1:00,2758,2848",
		"2011/3/26,2:00,2674,2767",
		"2011/3/26,3:00,2603,2707",
		"2011/3/26,4:00,2572,2696",
		"2011/3/26,5:00,2610,2828",
		"2011/3/26,6:00,2715,3095",
		"2011/3/26,7:00,2883,3351",
		"2011/3/26,8:00,3031,3500",
		"2011/3/26,9:00,3119,3575",
		"2011/3/26,10:00,3112,3491",
		"2011/3/26,11:00,3088,3435",
		"2011/3/26,12:00,2982,3223",
		"2011/3/26,13:00,3015,3329",
		"2011/3/26,14:00,2976,3318",
		"2011/3/26,15:00,2960,3303",
		"2011/3/26,16:00,3011,3388",
		"2011/3/26,17:00,3140,3451",
		"2011/3/26,18:00,3433,3480",
		"2011/3/26,19:00,3462,3283",
		"2011/3/26,20:00,0,3344",
		"2011/3/26,21:00,0,3261",
		"2011/3/26,22:00,0,3241",
		"2011/3/26,23:00,0,3080"
		};

		List<DenryokuBean> list =getDenryokuList(csvDatas);


		System.out.println();


	}

}
